package cn.beyond.vehicleManagement.controller;

import cn.beyond.vehicleManagement.pojo.VehicleDeprecition;
import cn.beyond.vehicleManagement.pojo.VehicleGasFiller;
import cn.beyond.vehicleManagement.pojo.Vehicles;
import cn.beyond.vehicleManagement.service.VehicleDeprecitionService;
import cn.beyond.vehicleManagement.service.VehicleGasFilterService;
import cn.beyond.vehicleManagement.util.PageUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns ={"/VehicleDeprectionServlet"} )
public class VehicleDeprectionServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //定义全局变量
        VehicleDeprecitionService service = new VehicleDeprecitionService();
        String opr = req.getParameter("opr");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        VehicleDeprecition vehicleDeprecition = new VehicleDeprecition();
        Map<String, Object> map = new HashMap<>();
        PrintWriter out = resp.getWriter();
        List<VehicleDeprecition> vehicleDeprecitionList = new ArrayList<>();
        System.out.println(opr);
        switch (opr) {
//            case "addvehicleProcurement":
//                boolean addflag= addUser(req,userService);
//                out.print(addflag);
//                break;
            //删除车辆采购记录
            case "deleteVehicleDeprection":
                int delId = Integer.parseInt(req.getParameter("Id"));
                boolean delflag = false;
                int delcount = service.deleteVehicleDeprecition(delId, "id");
                System.out.println(delcount);
                if (delcount > 0) {
                    delflag = true;
                }
                out.print(delflag);
                break;
//            case "updatevehicleProcurement":
//                updateUser(req,userService,out);
//
//                break;
            case "selectVehicleDeprection":

                break;
            case "listVehicleDeprection":
                //列举出折旧车辆列表
                vehicleDeprecitionList = service.selectUserByCondition(vehicleDeprecition, map);

                Vehicles vehicles=new Vehicles();
                List<Vehicles>vehiclesList= PageUtil.getPageUtil(1,100,"vehicles"
                        ,vehicles);
                List<Object>listmap=new ArrayList<>();
                listmap.add(vehicleDeprecitionList);
                listmap.add(vehiclesList);
                String listJosn = JSON.toJSONString(listmap);
                out.write(listJosn);
                map.clear();
                break;

        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
